import axios from 'axios'
import {AxiosStatus} from 'axios-status'
import router from '../../src/router';
const axiosStatus = new AxiosStatus({
  timeout: 20,
  autoRetry: false
})
axiosStatus.on('busy', (val) => {
  // console.log("busy", val);
})
axios.defaults.headers.post['Content-Type'] = "application/json";
axios.defaults.timeout = 5000;
var $router = "";
var setRouter = function(router){
  $router = router;
};
axiosStatus.register(axios);
async function doRequest(url, opts = {},return_all_data) {
    if (!opts['headers']) {
        opts['headers'] = {}
    }
    return axiosStatus.request({
        url: url,
        method: opts['method'] || 'get',
        baseURL: opts['baseURL'],
        headers: opts['headers'],
        params: opts['params'],
        data: opts['data'],
        timeout: 30000,
        withCredentials: false,
        cancelToken: opts['cancelToken'],
        success: (res) => {
			try {
				if (res.status != 200) {
					//return ('呜~ 请求超时啦, 请刷新重试～～');
					return {
						code: res.status,
						msg: res.statusText,
            state:0,
            data:[]
					}
				}else{
          if(return_all_data) return res.data;
					if (res.data.state == 3) {
            return $router.push("/login/login");
					}else if(res.data.state == 'gourl'){
						localStorage.setItem('wq_Role_Msg',res.data.msg);
            return $router.push("/project/role");

					}
				}
				return res.data
			} catch (error) {
				console.log('in try-catch', error)
			}
        },
        error : (err) => {
          return Promise.reject(err);
        }
    })
}

export {
  doRequest,
  setRouter
}
